home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best of MacTutor - S…e Code for Volumes 1 to 5
/
The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin
/
Source Code
/
#26 (Nov 87)
/
asm lab Formatter
/
NumToString.ROM.asm
< prev
Wrap
Assembly Source File
|
1987-10-21
|
2KB
|
68 lines
; NOTE: This code is Apple Computer's. Only the comments are mine.
;==========
NumToString
;==========
; convert a 32 bit longint into a pascal string
; input: D0 longint
; A0 points to a space of at least 12 bytes
; output: A0 points to pascal string
MOVEM.L D0-D6/A1,-(SP)
MOVEQ #0,D1 ;init digits
MOVEQ #0,D2
MOVEQ #0,D3
MOVEQ #0,D4
MOVEQ #0,D5
MOVEQ #31,D6 ;loop counter
LEA 1(A0),A1 ;skip length byte
TST.L D0 ;is num zero?
BGT.S @2
BMI.S @1
MOVE.B #'0',(A1)+ ;special case for zero
BRA.S @3
@1 MOVE.B #'-',(A1)+ ;give string a minus sign
NEG.L D0 ;make number positive
@2 ADD.L D0,D0 ;shift a bit into extend flag
ABCD D5,D5 ;tens' & ones' digits
ABCD D4,D4 ;thous' & hunds' digits
ABCD D3,D3 ;hund thous' & ten thous' digits
ABCD D2,D2 ;ten mils' & mils' digits
ABCD D1,D1 ;bils' & hund mils' digits
DBRA D6,@2 ;do next bit
;NOTE: at this point D6 = -1. It is used as a flag to kill leading zeros.
BSR.S Do2Digits
MOVE.B D2,D1
BSR.S Do2Digits
MOVE.B D3,D1
BSR.S Do2Digits
MOVE.B D4,D1
BSR.S Do2Digits
MOVE.B D5,D1
BSR.S Do2Digits
;calculate length of string that was created
@3 MOVE A1,D0 ;end of string + 1
SUB A0,D0 ;beginning of string
SUBQ.B #1,D0 ;minus 1 for length byte
MOVE.B D0,(A0)
MOVEM.L (SP)+,A1/D0-D6
RTS
Do2Digits
;convert BCD byte in D1 into 2 ASCII digits.
;do most significant digit
ROR #4,D1
BSR.S DoADigit
;do least significant digit
ROL #4,D1
DoADigit
TST D6 ;have we had a non-zero digit yet?
BPL.S @6
TST.B D1 ;is this a leading zero?
BEQ.S @7
MOVEQ #0,D6 ;print all zeros from now on
@6 ORI.B #$30,D1 ;covert BCD digit to ASCII
MOVE.B D1,(A1)+ ;add it to the string
SUB.B D1,D1
@7 RTS